Below are most of the Ruby statements I run in the "irb" console.
You should try them yourself and examine the output.


lecture1.irb_history :

# ------------------------------------------
# "Primitive" types like numbers, text
# Some basic math & comparison operations
# Variables as named places where data is stored
# ------------------------------------------
45 + 7
aa = 45 + 7
aa
someString = 'this is not interpolated #{aa}'
puts someString
someString = "this is interpolated #{aa}"
aRange = 10 .. 13
aRange.last
aRange.first
aRange.include?(12)
aRange.include?(120)
2*4
2**4
4 > 3
4 > 30
4 == 4
4 == "4"
# Of niche interest: "rocketship" operator for sorting/comparison
4 <=> 2
4 <=> 12
4 <=> 0
4 <=> 4
4 == "4".to_i
4.to_s
"a string".size
"a string".reverse
puts aa
# ------------------------------------------
# String interpolation via ""
# No interpolation via '' (doesn't look for #{} fields to process, treats as plain text)
# ------------------------------------------
someString = "this is interpolated #{Time.now}"
someString = "this is interpolated #{'a string'.size}"
andrew
someString = "this is interpolated #{andrew}"
someString = "this is interpolated 'andrew'"
someString = 'straight text #{Time.now}'
# ------------------------------------------
# Arrays - a list, access values by offset index
# ------------------------------------------
anArray = Array.new()
anArray.size
anArray.empty?
# Push two items into the array (push adds to the end of the array)
anArray.push(aa)
anArray.push(someString)
anArray[1]
anArray.first
anArray.last
anArray.include?(52)
anArray.unshift(3.14)
anArray
anArray.reverse
# This next will fail, read error message:
anArray[0].reverse
newArray = anArray.reverse
newArray
# In-place edit version of reverse() is reverse!()
anArray.reverse!
anArray.push(68)
anArray << 68.42
anArray.pop()
anArray.unshift(68)
# This next will fail, read error message
anArray.shift(68)
# shift() doesn't take an argument (it's for removing an item from the
# beginning of an array, hence the error message)
anArray.shift()
# Combine arrays:
aSecondArray = [ Time.now, "45%" ]
anArray + aSecondArray
mergedArray = anArray + aSecondArray
mergedArray.first
mergedArray.last
# Can "+" String too:
"text1" + "text23"
anArray[0]
puts anArray[0]
# ------------------------------------------
# Hashes - a map, access values by key
# ------------------------------------------
aHash = {}
aHash.size
aHash.empty?
aHash.key?("BRCA1")
aHash["BRCA1"] = 68.52
puts "The score is #{aHash['BRCA1']}"
# ------------------------------------------
# Parsing delimited data, other String manipulation
# ------------------------------------------
someString
someString.upcase
someString.upcase!
# Recall: in interpolated Strings, escape sequences like \n for newline,
# \t for tab, \\ for backslash itself, \#{} to ignore a #{} field at treat
# as literal characters.
aStr = "line1\nline2\ncol1\tcol2"
puts aStr
# gets() will get some input (line of text) from the user
# (technically, from stardard input stream, stdin)
inputFromARJ = gets
inputFromARJ
# Chomp removes trailing newline...and by default, removes all trailing whitespace
inputFromARJ.chomp
inputFromARJ.chomp!
# Stringing methods together...reverse complement say:
dna = "aaacCCGGT"
dna.reverse
dna.reverse.tr("actgACTG", "tgacTGAC")
dna.reverse.tr("actgACTG", "tgacTGAC").size
print "some line"
puts "some line"
# ------------------------------------------
# Flow control: looping, iteration, conditionally executed statments
# ------------------------------------------
# Watch out...Ctrl-c to interrupt infinite loop:
# loop {
#  puts "a line"
# }
counter = 0
loop {
  break if(counter > 10)
  puts "line"
  counter += 1
}
counter
# Iterate over each element in an array (put element data into "item" variable)
anArray
anArray.each { |item| puts "the item is #{item}"  }
anArray.each { |item|
  modified = "---> #{item}"
  puts modified
}
anArray.size
3.times { |ii| puts ii }
anArray.size.times { |ii|
  jj = ii + 1
  puts "#{jj}. #{anArray[ii]}"
}
# Iterate over each index rather than each element:
anArray.each_index { |ii|
  puts ii
}
# Iteration with respect to Hashes:
aHash
aHash["BRCA2"] = "80.1"
aHash
aHash.each { |aKey, aVal|
  puts "#{aKey} => #{aVal}"
}
aHash.each_key { |kkkk| puts kkkk }
3.upto(13) { |xx| puts xx }
anArray[8] = Time.now
# Should throw an error when hits a nil element in the array:
anArray.each { |xx|
  puts xx * 2
}
anArray.shift
anArray.pop
anArray << 90.1
# Use conditional flow to avoid doing bad things with nil:
anArray.each { |xx|
  if(xx.nil?)
    puts "n/a"
  else
    puts xx * 2
  end
}
anArray.each { |xx|
  if(xx.nil?)
    puts "n/a"
  elsif(xx.is_a?(Fixnum))
    puts "not a float"
  else
    puts "probably a float"
  end
}
puts "ok" if(counter > 2 and counter < 10)
puts "ok" if(counter > 2 and counter < 12)
puts "ok" if(counter > 2 or counter < 10)
anArray.each { |xx|
  if(!xx.nil?)
    yy = xx ** 2
    puts yy
  end
}
# "unless" works as "if(! ______)"; thus, alternative iteration is:
anArray.each { |xx|
  unless(xx.nil?)
    yy = xx ** 2
    puts yy
  end
}
# ------------------------------------------
# Basic File I/O
# ------------------------------------------
# refFlat.lff is a file; path not needed if in same directory you are
# running code in. You can use any LFF file for example here
# ...download one from genboree or galaxy
lffFile = File.open("refFlat.lff")
# Watch out, if you have refFlat.lff, this will put every line to the screen:
#lffFile.each_line { |line|
#  puts line
#}
lffFile.lineno
lffFile.rewind
lffFile.each_line { |line|
  puts line.size
  break if(lffFile.lineno > 10)
}
aLine = lffFile.readline
# Let's parse that line. It is tab-delimited so
# tell split() to chop up text based on the tab char ("\t")
lffArray = aLine.split("\t")
lffArray.each { |xx| puts xx }
lffArray.last
# Better to chomp the line first, to be safe
lffArray = aLine.chomp.split("\t")
asArray = line.split("\t")
puts asArray.last
asArray = line.chomp.split("\t")
# Will read whole file into memory (could take time...)
wholeFile = lffFile.read ; puts
wholeFile.size
# Using strings as if they were I/O objects like Files are:
require 'stringio'
sio = StringIO.new(wholeFile)
sio.size
sio.readline
aLine = sio.readline
# ------------------------------------------
# Regular expressions - simple examples
# ------------------------------------------
asArray = aLine.chomp.split("\t")
asArray.last
# Regexp matches via =~ return index of first match in the string
asArray.last =~ /NP_/
asArray.last =~ /N[MP]_/
asArray.last =~ /\d/
# () are "capture groups" and will capture matching text and put
# into $1 variable for first capture group, $2 for 2nd capture group, etc:
asArray.last =~ /(\d)/
puts $1
asArray.last =~ /(\d+)/
puts $1
asArray.last =~ /pro[^t]+/
asArray.last =~ /(pro[^t]+)/
asArray.last =~ /(pro[^t]+)(.+)/
puts $2
# When doesn't match, returns nil:
asArray.last =~ /(asddsfadsfas[^t]+)(.+)/
puts "oK" if(asArray.last =~ /(asddsfadsfas[^t]+)(.+)/)
puts "oK" if(asArray.last =~ /(pro[^t]+)(.+)/)